﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;

/*
我用C#做的WebService中有几个WebMethod需要使用out和ref参数，但是在WebService   Behavior中不知道怎么调用这样的WebMethod。   
其实很简单，在调用的入口处，ref与普通参数一样写，out参数不用写。在返回值时，用result.value["参数名"]来去参数，用result.value[WebMethod的名字   +   "Result"]来取返回值结果。
*/
public partial class flt_avail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            lblError.Text = "";
            if (IsRobot.Test("Query", 1, 60, 6))
            {
                lblError.Text = "Please don't query continually!";
                return;
            }
            return;
        }
        StringBuilder sFlights = new StringBuilder();
        int trip_count,i,adt,chd,inf;

        if (Session["TRIP_COUNT"] == null)
        {
            btnNext.Enabled = false;
            lblError.Text = "非法页面调用!";
            return;
        }
        trip_count = Convert.ToInt16(Session["TRIP_COUNT"].ToString());


        adt = Convert.ToInt16(Session["ADT_COUNT"].ToString());
        chd = Convert.ToInt16(Session["CHD_COUNT"].ToString());
        inf = Convert.ToInt16(Session["INF_COUNT"].ToString());
        if (adt < 0 || chd < 0 || inf < 0 || (adt < inf) || adt + chd > 50)
        {
            lblError.Text = "乘客人数过多!";
            btnNext.Enabled = false;
            return;
        }


        string[] ori = new string[trip_count + 1];
        string[] des = new string[trip_count + 1];
        string[] trip_dates = new string[trip_count];
        for (i = 1; i <= trip_count; i++)
        {
            ori[i - 1] = Session["lstOriCity" + i.ToString()].ToString();
            des[i - 1] = Session["lstDesCity" + i.ToString()].ToString();
            trip_dates[i - 1] = Session["txtFltDate" + i.ToString()].ToString();
        }

        CRSI.Query instFlt = new CRSI.Query();
        DataSet dsFlgiths;
        int ret_code;
        string ret_msg;
        string sign;

        try
        {
            for (i = 1; i <= trip_count; i++)
            {
                sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(ori[i - 1] + "|" + des[i - 1] + "|" + Convert.ToDateTime(trip_dates[i - 1]).ToString("yyyyMMdd") + "|" + Application["HASH_PWD"].ToString(), "MD5");
                dsFlgiths = instFlt.QueryAvailFlights(ori[i - 1], des[i - 1], 2, Convert.ToDateTime(trip_dates[i - 1]), Application["UID"].ToString(), sign, out ret_code, out ret_msg);
                if(ret_code!=0)
                {
                    if (ret_code == -1)
                    {
                        Response.Write(ret_msg);
                        ApplicationInstance.CompleteRequest();
                        return;
                    }
                    else if (ret_code == 91)
                    {
                        Response.Write("API unauthorized!");
                        ApplicationInstance.CompleteRequest();
                        return;
                    }
                    else if (ret_code == 1)
                    {
                        Response.Write("City timezone not found!");
                        ApplicationInstance.CompleteRequest();
                        return;
                    }
                    else if (ret_code == 2)
                    {
                        Response.Write("Check trip time!");
                        ApplicationInstance.CompleteRequest();
                        return;
                    }
                    else
                    {
                        Response.Write("Error " + ret_code.ToString());
                        ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
                   
                sFlights.Append("<table><b><tr><td>"+i.ToString()+".</td><td>"+ori[i - 1]+"</td><td>"+des[i - 1]+"</td><td>"+ Convert.ToDateTime(trip_dates[i - 1]).ToString("yyyyMMdd")+"</td></tr></b></table>");
                StringBuilder tmp = new StringBuilder();
                int row_count = 0;
                if (dsFlgiths.Tables[0].Rows.Count == 1 && Convert.IsDBNull(dsFlgiths.Tables[0].Rows[0]["flt_no"]))//无航班
                {
                    sFlights.Append("<table><tr><td>No flight.</td></tr></table>");
                }
                else
                {
                    foreach (DataRow row_flights in dsFlgiths.Tables[0].Rows)
                    {
                        row_count++;
                        tmp.Append("flt_avail_fares.aspx?airline=" + row_flights["airline"].ToString() + "&flt_no=" + row_flights["flt_no"].ToString() + "&flt_date="
                            + row_flights["flt_date"].ToString() + "&ori=" + row_flights["ori_city"].ToString() + "&ori_airport=" + row_flights["ori_airport"].ToString()
                            + "&des=" + row_flights["des_city"].ToString() + "&des_airport=" + row_flights["des_airport"].ToString() + "&dep_time=" + row_flights["dep_time"].ToString() + "&arr_time=" + row_flights["arr_time"].ToString()
                            + "&lang=2&seats=" + (adt + chd).ToString() + "&tax_type=" + row_flights["tax_type"].ToString() + "&international=" + row_flights["international"].ToString());
                        tmp.Append("&seg_sn=" + i.ToString() + "&seg_total=" + Session["TRIP_COUNT"].ToString());
                        sFlights.Append("<table><tr><td width='10px'></td><td><button type='button' onMouseOver=\"QFltAvailFares('" + i.ToString() + "','" + row_count.ToString() + "','" + tmp.ToString() + "');\">></button></td><td>"
                            + Convert.ToDateTime(row_flights["dep_time"]).ToShortTimeString() + "</td><td>" + Convert.ToDateTime(row_flights["dep_time"]).ToShortTimeString()
                            + "</td><td>" + row_flights["airline"] + row_flights["flt_no"]);
                        if (row_flights["direct_fly"].ToString() == "0")
                            sFlights.Append("(Stop)");
                        sFlights.Append("</td><td>" + row_flights["airline_name"] + "</td><td>" + row_flights["ac_type"] + "</td><td>" + row_flights["ori_apt_name"]
                            + "(" + row_flights["ori_terminal"] + ") - " + row_flights["des_apt_name"] + "(" + row_flights["des_terminal"] + ")</td></tr></table>");
                        sFlights.Append("<div id=\"flt_" + i.ToString() + "_" + row_count.ToString() + "\"></div><br/>");
                        tmp.Clear();
                    }
                }
                dsFlgiths.Clear();

            }
            litFlights.Text = sFlights.ToString();

        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            btnNext.Enabled = false;
        }

    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        
    }



}
